/**
 * Created by CK on 2016/11/8.
 */
if(window.innerHeight < 800){
    var el = document.getElementById('xh_crumb');
    removeElement(el);
}
function removeElement(_element){
    var _parentElement = _element.parentNode;
    if(_parentElement){
        _parentElement.removeChild(_element);
    }
}
$(document).ready(function(){
    var appCode     = xhTool.getQuery('iAppCode'),
        list        = $('#List'),
        lay         = $('#lay_list'),
        startDom    = $('#List_Search_StartTime'),
        endDom      = $('#List_Search_EndTime'),
        schoolDom   = $('#List_Search_SchoolName'),
        searchDpm   = $('#bt_list_search'),
        isSort      = false;

    startDom.datebox('setValue', GetDateStr(-7));
    endDom.datebox('setValue', GetDateStr(-1));

    var startTime   = startDom.getElementValue(),
        endTime     = endDom.getElementValue(),
        queryParams = {
            iAppCode: appCode,
            iStartTime: startTime,
            iEndTime: endTime
        };

    list.initDataGrid(baseUrl + 'Stats/getSchoolOverview', {queryParams: queryParams});
    searchDpm.click(function(){
        getParams();
        list.addDataGridParams(queryParams);
    });
    xhTool.bindDocKeyDown(searchDpm);

    DataGrid.onDblClickRow = function(rowIndex, rowData){
        clearTimeout(clickTimer);
        var tabName = '统计_' + rowData.sSchoolName.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,"");
        var url = baseUrl + 'Stats/singleSchoolInfo?iAppCode=' + appCode + '&iSchoolId=' + rowData.iSchoolId
            + '&iStartTime=' + startTime + '&iEndTime=' + endTime;
        if(rowData.sSubject){
            tabName = tabName + '_' + rowData.sSubject;
            url = url + '&sSubject=' + rowData.sSubject;
        }
        window.top.xhTop.createNewTab(tabName, encodeURI(url));
    };
    DataGrid.onLoadSuccess = function(data){
        if (data.rows.length <= 0) return false;
        list.mergeCellsByField(['sSchoolName']);
        if(!isSort){
            if(lay.layout('panel', 'north')[0].clientHeight == 0){
                lay.layout('expand', 'north');
            }
            changeSelectRender();
        }
        isSort = false;
    };
    DataGrid.onSortColumn = function(sort, order){
        isSort = true;
    };
    $('#bt_list_Export').click(function(){
        getParams();
        location.href = baseUrl + 'Stats/exportSchoolOverview?' + parseParam(queryParams);
        // window.open(baseUrl + 'Stats/exportSchoolOverview?' + parseParam(queryParams));
        list.addDataGridParams(queryParams);
    });
    function getParams(){
        startTime = startDom.getElementValue();
        endTime   = endDom.getElementValue();
        if(!startTime || !endTime){
            alert('时间不可为空！');
            return false;
        }
        queryParams.iStartTime = startTime;
        queryParams.iEndTime   = endTime;
        queryParams.sSchoolName= schoolDom.getElementValue();
    }
    //云作业科目
    $('#bt_list_SeeSubject').click(function(){
        var url = baseUrl + 'Stats/schoolSubjectOverview?iAppCode=' + appCode
            + '&iStartTime=' + startTime + '&iEndTime=' + endTime;
        window.top.xhTop.createNewTab('统计_云作业科目', url);
    });
    $('#period').combobox({
        editable: false,
        panelHeight: 'auto',
        onChange: function(newValue, oldValue){
            queryParams.period = newValue;
            renderAppUseChart(queryParams);
        }
    });

    DataGrid.onSelect = function(){
        changeSelectRender();
    };
    DataGrid.onUnselect = function(){
        changeSelectRender();
    };
    DataGrid.onSelectAll = function(){
        changeSelectRender();
    };
    DataGrid.onUnselectAll = function(){
        changeSelectRender();
    };
    var clickTimer; //防止过快切换
    function changeSelectRender(){
        clearTimeout(clickTimer); //规定时间内点击则清除，防止请求过于频繁
        clickTimer = setTimeout(function() {
            if(lay.layout('panel', 'north')[0].clientHeight == 0){
                lay.layout('expand', 'north');
            }
            var schoolIds = list.getDataGridSelectedFields('iSchoolId');
            var schoolParams = queryParams;
            schoolParams.schoolIds = schoolIds;
            renderAppUseChart(schoolParams);
            if(schoolIds.indexOf(',') > 0){
                renderSchoolTrendChart(schoolParams);
            }
            else{
                renderSchoolTrendChart(queryParams);
            }
        }, 1000);
    }
    function renderAppUseChart(params){
        var source = {url: baseUrl + 'Stats/getChartSchoolOverview'};
        if(params){
            source.params = params;
        }
        (new xhChart()).line('chart', source, {showTool: false, xFontSize: 10});
    }
    function renderSchoolTrendChart(params){
        var source = {url: baseUrl + 'Stats/getChartSchoolTrend'};
        if(params){
            source.params = params;
        }
        (new xhChart()).pie('chart_trend', source, {showSeriesInfo: false});
    }
});
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//获取当前月份的日期
    var d = dd.getDate();
    return y+"-"+m+"-"+d;
}
var parseParam = function(param, key){
    var paramStr="";
    if(param instanceof String||param instanceof Number||param instanceof Boolean){
        paramStr+="&"+key+"="+encodeURIComponent(param);
    }else{
        $.each(param,function(i){
            var k=key==null?i:key+(param instanceof Array?"["+i+"]":"."+i);
            paramStr+='&'+parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};